package com.woniu113.junk.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.site.client.UserClient;
import com.woniu113.junk.site.client.dto.AdminInfo;
import com.woniu113.junk.site.client.dto.UserInfo;
import com.woniu113.junk.site.dto.SuggestionDTO;
import com.woniu113.junk.site.exception.SuggestionException;
import com.woniu113.junk.site.model.Notice;
import com.woniu113.junk.site.model.Suggestion;
import com.woniu113.junk.site.mapper.SuggestionMapper;
import com.woniu113.junk.site.param.suggestion.AddSuggestionParam;
import com.woniu113.junk.site.param.suggestion.AllSuggestionParam;
import com.woniu113.junk.site.service.SuggestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.util.Data;
import org.springframework.beans.FatalBeanException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class SuggestionServiceImpl extends ServiceImpl<SuggestionMapper, Suggestion> implements SuggestionService {

    @Resource
    private SuggestionMapper suggestionMapper;
    @Resource
    private UserClient userClient;

    @Override
    public Suggestion addSuggestion(AddSuggestionParam param, Integer userId) {
        //1.判断数据完整性
        if (param.getSuggestionContent()==null ||param.getUserType()==null ||userId==null){
            throw new SuggestionException("请将数据填写完整",7001);
        }
        //2.执行新增操作
        Suggestion suggestion = new Suggestion();
        if (param.getUserType()==Data.SUGGESTION_USER){//当建议是用户建议时
            UserInfo userInfo = userClient.selectIntegral(userId).getData();
            if (userInfo.getUserState()!=Data.USER_NORMAL){//判断用户状态是否正常
                throw new SuggestionException("该用户状态异常,不能提建议",7002);
            }
            suggestion.setUserName(userInfo.getUserName());
            suggestion.setUserPhone(userInfo.getUserPhone());
        }else if (param.getUserType()== Data.SUGGESTION_ADMIN){//当建议是管理员建议时
            AdminInfo adminInfo = userClient.selectAdminById(userId).getData();
            if (adminInfo.getAdminState()!=Data.ADMIN_NORMAL){//判断该管理员状态是否正常
                throw new SuggestionException("该管理员状态异常,不能提建议",7003);
            }
            suggestion.setUserName(adminInfo.getAdminName());
            suggestion.setUserPhone(adminInfo.getAdminPhone());
        }
        suggestion.setSuggestionContent(param.getSuggestionContent());
        suggestion.setCreateTime(LocalDateTime.now());
        suggestion.setUserId(userId);
        suggestion.setUserType(param.getUserType());
        suggestionMapper.insert(suggestion);
        return suggestion;
    }

    @Override
    public SuggestionDTO findAll(AllSuggestionParam param, Integer offset) {
        QueryWrapper<Suggestion> queryWrapper = new QueryWrapper<>();
        if (param.getUserType()!=null){
            queryWrapper.eq("user_type",param.getUserType());
        }
        int size = Math.toIntExact(suggestionMapper.selectCount(queryWrapper)); // 获取符合条件的总记录数
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<Suggestion> infos = suggestionMapper.selectList(queryWrapper);
        SuggestionDTO dto = new SuggestionDTO();
        dto.setList(infos);
        dto.setTotal(size);
        return dto;
    }


}
